package com.fileupload.process.strategy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;

/**
 * 删除文件策略
 * 上传完成后删除源文件
 * @author 小乙
 */
public class DeleteFileStrategy extends AbstractFileFinishActionStrategy {
    
    private static final Logger logger = LoggerFactory.getLogger(DeleteFileStrategy.class);

    /**
     * 默认构造函数
     */
    public DeleteFileStrategy() {
        super("delete");
    }
    
    /**
     * 构造函数
     * 
     * @param monitorDir 监控目录
     */
    public DeleteFileStrategy(String monitorDir) {
        super("delete", monitorDir);
    }
    
    @Override
    public boolean execute(File sourceFile, String targetDir) {
        logger.debug("Deleting file: {}", sourceFile.getAbsolutePath());
        
        if (sourceFile == null || !sourceFile.exists()) {
            logger.warn("File does not exist: {}", sourceFile);
            return false;
        }
        
        try {
            boolean deleted = sourceFile.delete();
            if (deleted) {
                logger.info("File deleted successfully: {}", sourceFile.getAbsolutePath());
                return true;
            } else {
                logger.error("Failed to delete file: {}", sourceFile.getAbsolutePath());
                return false;
            }
        } catch (SecurityException e) {
            logger.error("Security exception when deleting file: {}", sourceFile.getAbsolutePath(), e);
            return false;
        }
    }
}
